include_guard(GLOBAL)
include(FetchContent)
option(BOTAN_INSTALLED "Botan installed" OFF)
# Проверяем, существует ли указанный путь
message("BOTAN_INSTALLED: ${BOTAN_INSTALLED}")
file(GLOB BOTAN_LIB_ROOT_DIR_CONTENTS "${BOTAN_LIB_ROOT_DIR}")

if(NOT BOTAN_LIB_ROOT_DIR_CONTENTS)
    message(WARNING "Directory '${BOTAN_LIB_ROOT_DIR}' does not exist. Creating it.")
    file(MAKE_DIRECTORY "${BOTAN_LIB_ROOT_DIR}")
endif()
set(BOTAN_LIB_REPOS_DIR   "${BOTAN_LIB_ROOT_DIR}/repos")

set(BOTAN_LIB_INSTALL_DIR "${BOTAN_LIB_ROOT_DIR}-install")
file(GLOB BOTAN_LIB_INSTALL_DIR_CONTENTS "${BOTAN_LIB_INSTALL_DIR}")
if(NOT BOTAN_LIB_INSTALL_DIR_CONTENTS)
    message(WARNING "Directory '${BOTAN_LIB_INSTALL_DIR}' does not exist. Creating it.")
    file(MAKE_DIRECTORY "${BOTAN_LIB_INSTALL_DIR}")
endif()

set(BOTAN_LIB_INCLUDE_DIR "${BOTAN_LIB_INSTALL_DIR}/include")
file(GLOB BOTAN_LIB_INCLUDE_DIR_CONTENTS "${BOTAN_LIB_INCLUDE_DIR}")
if(NOT BOTAN_LIB_INCLUDE_DIR_CONTENTS)
    message(WARNING "Directory '${BOTAN_LIB_INCLUDE_DIR}' does not exist. Creating it.")
    file(MAKE_DIRECTORY "${BOTAN_LIB_INCLUDE_DIR}")
endif()

set(BOTAN_LIB_BIN_DIR     "${BOTAN_LIB_INSTALL_DIR}/bin")
file(GLOB BOTAN_LIB_BIN_DIR_CONTENTS "${BOTAN_LIB_BIN_DIR}")
if(NOT BOTAN_LIB_BIN_DIR_CONTENTS)
    message(WARNING "Directory '${BOTAN_LIB_BIN_DIR}' does not exist. Creating it.")
    file(MAKE_DIRECTORY "${BOTAN_LIB_BIN_DIR}")
endif()

set(BOTAN_LIB_LIB_DIR     "${BOTAN_LIB_INSTALL_DIR}/lib")
file(GLOB BOTAN_LIB_LIB_DIR_CONTENTS "${BOTAN_LIB_LIB_DIR}")
if(NOT BOTAN_LIB_LIB_DIR_CONTENTS)
    message(WARNING "Directory '${BOTAN_LIB_LIB_DIR}' does not exist. Creating it.")
    file(MAKE_DIRECTORY "${BOTAN_LIB_LIB_DIR}")
endif()
if(NOT BOTAN_INSTALLED)
    set(${BOTAN_VERSION} 3.3.0)
    file(GLOB BOTAN_LIB_ROOT_DIR_CONTENTS "${BOTAN_LIB_ROOT_DIR}")

    if(NOT BOTAN_LIB_ROOT_DIR_CONTENTS)
        message(WARNING "Directory '${BOTAN_LIB_ROOT_DIR}' does not exist. Creating it.")
        file(MAKE_DIRECTORY "${BOTAN_LIB_ROOT_DIR}")
    endif()
    set(BOTAN_LIB_REPOS_DIR   "${BOTAN_LIB_ROOT_DIR}/repos")

    set(BOTAN_LIB_INSTALL_DIR "${BOTAN_LIB_ROOT_DIR}-install")
    file(GLOB BOTAN_LIB_INSTALL_DIR_CONTENTS "${BOTAN_LIB_INSTALL_DIR}")
    if(NOT BOTAN_LIB_INSTALL_DIR_CONTENTS)
        message(WARNING "Directory '${BOTAN_LIB_INSTALL_DIR}' does not exist. Creating it.")
        file(MAKE_DIRECTORY "${BOTAN_LIB_INSTALL_DIR}")
    endif()

    set(BOTAN_LIB_INCLUDE_DIR "${BOTAN_LIB_INSTALL_DIR}/include")
    file(GLOB BOTAN_LIB_INCLUDE_DIR_CONTENTS "${BOTAN_LIB_INCLUDE_DIR}")
    if(NOT BOTAN_LIB_INCLUDE_DIR_CONTENTS)
        message(WARNING "Directory '${BOTAN_LIB_INCLUDE_DIR}' does not exist. Creating it.")
        file(MAKE_DIRECTORY "${BOTAN_LIB_INCLUDE_DIR}")
    endif()

    set(BOTAN_LIB_BIN_DIR     "${BOTAN_LIB_INSTALL_DIR}/bin")
    file(GLOB BOTAN_LIB_BIN_DIR_CONTENTS "${BOTAN_LIB_BIN_DIR}")
    if(NOT BOTAN_LIB_BIN_DIR_CONTENTS)
        message(WARNING "Directory '${BOTAN_LIB_BIN_DIR}' does not exist. Creating it.")
        file(MAKE_DIRECTORY "${BOTAN_LIB_BIN_DIR}")
    endif()

    set(BOTAN_LIB_LIB_DIR     "${BOTAN_LIB_INSTALL_DIR}/lib")
    file(GLOB BOTAN_LIB_LIB_DIR_CONTENTS "${BOTAN_LIB_LIB_DIR}")
    if(NOT BOTAN_LIB_LIB_DIR_CONTENTS)
        message(WARNING "Directory '${BOTAN_LIB_LIB_DIR}' does not exist. Creating it.")
        file(MAKE_DIRECTORY "${BOTAN_LIB_LIB_DIR}")
    endif()

    set(BOTAN_LIB_CONFIGURE_PY_ARGS         "--prefix=${BOTAN_LIB_INSTALL_DIR}")
    list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--includedir=${BOTAN_LIB_INCLUDE_DIR}")
    list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--bindir=${BOTAN_LIB_BIN_DIR}")
    if (MSVC)
        list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--cc=msvc")
        list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--os=windows")
        list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--build-tool=ninja")

    endif(MSVC)
    if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
        list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--cc=gcc")
        list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--os=mingw")
        list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--build-tool=ninja")
    endif()
    list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--libdir=${BOTAN_LIB_LIB_DIR}")
    list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--build-targets=static")
    list(APPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--enable-modules=aes,aes_armv8,base32,base64,kuznyechik,gost_28147,gost_3410,gost_3411")
    list(PREPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--disable-shared-library")

    if(MSVC OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
        set(BOTAN_LIB_MAKE_COMMAND    "ninja")
        set(BOTAN_LIB_TEST_COMMAND    "ninja;check")
        set(BOTAN_LIB_INSTALL_COMMAND "ninja;install")
    else()
        set(BOTAN_LIB_MAKE_COMMAND    "make")
        set(BOTAN_LIB_TEST_COMMAND    "make;check")
        set(BOTAN_LIB_INSTALL_COMMAND "make;install")
    endif()

    if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
        list(PREPEND BOTAN_LIB_CONFIGURE_PY_ARGS "--debug-mode")
    endif()
    find_package(Python3 REQUIRED)
    include(FetchContent)
    FetchContent_Declare(
        botan
        GIT_REPOSITORY  https://github.com/randombit/botan.git
        GIT_TAG         ${BOTAN_VERSION}
        PREFIX          ${BOTAN_LIB_ROOT_DIR}
        SOURCE_DIR      ${BOTAN_LIB_REPOS_DIR}
    )

    message("Botan lib lib dir: ${BOTAN_LIB_LIB_DIR}")
    set(FETCHCONTENT_QUIET OFF CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(botan)
    if(${botan_POPULATED} AND NOT EXISTS ${BOTAN_LIB_LIB_DIR}/libbotan-3.a)
        find_package(Python3 COMPONENTS Interpreter)
        if(NOT ${Python3_Interpreter_FOUND})
          message(FATAL_ERROR "Python3 Interpreter NOT FOUND")
        endif()

        execute_process(
          COMMAND           ${Python3_EXECUTABLE} configure.py ${BOTAN_LIB_CONFIGURE_PY_ARGS}
          WORKING_DIRECTORY ${BOTAN_LIB_REPOS_DIR}
          COMMAND_ECHO      STDOUT
        )
        execute_process(
          COMMAND           ${BOTAN_LIB_MAKE_COMMAND}
          WORKING_DIRECTORY ${BOTAN_LIB_REPOS_DIR}
          COMMAND_ECHO      STDOUT
        )
        # execute_process(
        #   COMMAND           ${BOTAN_LIB_TEST_COMMAND}
        #   WORKING_DIRECTORY ${BOTAN_LIB_REPOS_DIR}
        #   COMMAND_ECHO      STDOUT
        # )
        execute_process(
          COMMAND           ${BOTAN_LIB_INSTALL_COMMAND}
          WORKING_DIRECTORY ${BOTAN_LIB_REPOS_DIR}
          COMMAND_ECHO      STDOUT
        )
        set(BOTAN_INSTALLED ON CACHE BOOL "Botan installed" FORCE)
    endif()
    message("BOTAN_INSTALLED: ${BOTAN_INSTALLED}")
endif(NOT BOTAN_INSTALLED)
